﻿<cfscript>
/** 
* @hint "bookBase - 教材ISBN唯一性验证" 
*/ 

component 
	extends="MachII.framework.EventFilter" 
	displayname="acadmicManager.filters.book.bookUniqueFilter" 
	output=false 
	depends="senateMathAdvice" 
	{

	/*
	* @hint "Configures this filter as part of the Mach-II framework" 
	*/
	public void function configure() output=false {} 
	
	/*
	* @hint "Filters event and returns a boolean to Mach-II indicating whether or not the event queue should proceed.  If not, the event queue is cleared and a new event is announced."
	* 
	* @event "MachII Event Object"
	* @eventContext "MachII EventContext Object"
	*/
	public boolean function filterEvent ( required MachII.framework.Event event, required MachII.framework.EventContext eventContext ) output=false {

		var bookId = event.getArg("BID");
		var isbn = trim( replace(event.getArg("ISBN"), "-", "", "ALL") );
		
		var checkArray = [];
		
		/* 自编教材 */
		if ( isbn eq "UUID" ) { return true; }
		
		if ( getsenateMathAdvice().isISBN(isbn) ) {
	
			if ( len(bookId) ) {
				/* 修改教材 */
				sql = "	SELECT
							b.bid
						FROM
							t_book b
						WHERE
							b.bid != :bid 
							AND
							b.book_isbn = :isbn ";
				
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name = "bid", value = bookId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "isbn", value = isbn, cfsqltype = "cf_sql_varchar");
				rs_check = queryObj.execute(sql = sql).getResult();
				
			} else { 
				/* 添加教材 */
				sql = "	SELECT
							b.bid
						FROM
							t_book b
						WHERE
							b.book_isbn = :isbn ";
							
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name = "isbn", value = isbn, cfsqltype = "cf_sql_varchar");
				rs_check = queryObj.execute(sql = sql).getResult();
			}

			if (rs_check.recordCount) {
				/* 如果已经存在ISBN编号 并且教材代码与当前不同 */
				event.setArg("bookExistsID", arrayToList(rs_check['bid']));
				announceEvent("bookExists", event.getArgs());
			} else {
				/* 如果ISBN不存在 则继续更新 */
				return true;
			}

		}
		
		return false;
	}
	
}

</cfscript>